package edu.math.chaos.params;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.Properties;

/**
 * User: Илья
 * Date: 21.11.13
 * Time: 21:10
 */
public class ApplicationParameters {

	private double x1;
	private double x2;
	private double y1;
	private double y2;
	private double h;
	private double cRe;
	private double cIm;
	private double n;

	public double getX1() {
		return x1;
	}

	public void setX1(double x1) {
		this.x1 = x1;
	}

	public double getX2() {
		return x2;
	}

	public void setX2(double x2) {
		this.x2 = x2;
	}

	public double getY1() {
		return y1;
	}

	public void setY1(double y1) {
		this.y1 = y1;
	}

	public double getY2() {
		return y2;
	}

	public void setY2(double y2) {
		this.y2 = y2;
	}

	public double getH() {
		return h;
	}

	public void setH(double h) {
		this.h = h;
	}

	public double getCRe() {
		return cRe;
	}

	public void setCRe(double cRe) {
		this.cRe = cRe;
	}

	public double getCIm() {
		return cIm;
	}

	public void setCIm(double cIm) {
		this.cIm = cIm;
	}

	public int getN() {
		return (int) n;
	}

	public void setN(int n) {
		this.n = n;
	}

	public static ApplicationParameters readParameters(String fileName) {
		Properties properties = new Properties();
		try {
			BufferedInputStream inputStream = new BufferedInputStream(new FileInputStream(fileName));
			properties.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
			return null;
		}

		ApplicationParameters parameters = new ApplicationParameters();
		Field[] fields = ApplicationParameters.class.getDeclaredFields();
		for (Field field : fields) {
			String stringValue = properties.getProperty(field.getName());
			if (stringValue == null) {
				continue;
			}

			try {
				Double value = Double.valueOf(stringValue);
				field.set(parameters, value);
			} catch (Throwable e) {
				continue;
			}
		}

		return parameters;
	}
}
